#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"

void add_new_isymbol(int position, char *name) {
	symbol_table[ symbol_table_counter ].type = INT_TYPE;
	symbol_table[ symbol_table_counter ].position = position;
	symbol_table[ symbol_table_counter ].name = strdup(name);
	symbol_table_counter += 1;
} 

void declare_var(char *name) {
	symbol_table[ symbol_table_counter ].name = strdup(name);
	symbol_table[ symbol_table_counter ].position = symbol_table_counter;
	symbol_table_counter += 1;
}


int get_position_by_name(char *name) {
	int i;

	for (i=0; i < symbol_table_counter; i++) {
		if ( !strcmp(name, symbol_table[i].name) ) {
			return symbol_table[i].position;
		}
	}

	if ( atoi(name) > 0 ) {
	} else {
		printf("Variavel com nome %s nao definida. Abortando!\n", name);
		exit(EXIT_FAILURE);
	}
}

void dump_symbol_table() {
	int i;

	printf("\n\n=========================================\n");
	printf("=              SIMBOLOS                 =\n");
	printf("=========================================\n");
	printf("Nome \t  Posicao\n");
	for (i=0; i < symbol_table_counter; i++) {
		if (symbol_table[i].type == INT_TYPE) {
			printf("%s \t %d\n", symbol_table[i].name, symbol_table[i].position);
		} else {
			printf("%s \t %d\n", symbol_table[i].name, symbol_table[i].position);
		}
	}
}
